var doUserInfo = {
    // 鑾峰彇鐢ㄦ埛淇℃伅
    getUserInfo: function getUserInfo() {
        var userInfo = localStorage.userInfo;
        if (userInfo) {
            return JSON.parse(userInfo);
        }
        return {};
    },

    // 璁剧疆鐢ㄦ埛淇℃伅
    setUserInfo: function setUserInfo(userInfo) {
        localStorage.userInfo = JSON.stringify(userInfo);
    },

    // 璁剧疆鐢ㄦ埛淇℃伅
    removeUserInfo: function removeUserInfo() {
        localStorage.removeItem('userInfo');
    },
    updateLoginUserInfo: function updateLoginUserInfo(id, callback) {
        requestAjax({
            url: '/customUser/getByIdCustom',
            data: { id: id },
            success: function success(data) {
                data.userHiddenPhone = data.mobilePhone.substr(0, 3) + '****' + data.mobilePhone.substr(7, 4);
                doUserInfo.setUserInfo(data);
                callback && callback(data);
            },
            error: function error() {
                //doToken.removeToken()
            }
        });
    }
};

// Ajax
function requestAjax(option) {
    var baseUrl = option.url || '';
    if (option.url) {
        if (option.url.indexOf('http') === -1) {
            baseUrl = 'https://wwwapi.vx56.com' + option.url;
        }
    }
    var params = option.data;
    $.ajax({
        url: baseUrl,
        type: option.type || 'post',
        data: params,
        processData: option.processData,
        contentType: option.contentType,
        async: option.async,
        success: function success(data) {
            if (option.isAll) {
                option.success && option.success(data);
                return;
            }
            if (data.code == 200) {
                option.success && option.success(data.data);
            } else {
                option.error && option.error();
            }
        },
        error: function error(_error) {}
    });
}
// 鏂囦欢涓婁紶
function uploadAjax(file, callback) {
    var formData = new FormData();
    formData.append('upfile', file);
    $.ajax({
        url: 'https://wwwapi.vx56.com/oss/file/upload/api',
        type: 'post',
        data: formData,
        processData: false,
        contentType: false,
        success: function success(data) {
            if (data.code == 0) {
                callback && callback(data.data);
            }
        },
        error: function error(_error2) {
            layer.msg('涓婁紶澶辫触锛佽閲嶆柊閫夋嫨鎴栫◢鍚庡啀璇曪紒');
        }
    });
}
function openLoading(dom, title) {
    var div = document.createElement('div');
    div.className = 'x-loading';
    div.style.width = '100%';
    div.style.height = '100%';
    div.style.backgroundColor = 'rgba(255, 255, 255, 1)';
    // div.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'
    div.style.left = '0';
    div.style.top = '0';
    div.style.zIndex = '9999';
    div.style.position = 'absolute';
    // dom.style.filter = 'blur(10px)'
    var isPosition = getComputedStyle(dom, false).position;
    if (!isPosition || isPosition == 'static') {
        dom.style.position = 'relative';
        dom.style.overflow = 'hidden';
    }
    document.body.style.overflow = 'hidden';
    div.innerHTML = '<img src="/public/images/loading.gif" class="x-loading-img" style="position: absolute;left: 0;right: 0;bottom: 0;top: 0;margin: auto;width: 40px;height: 40px;"><div style="text-align: center;color: #666;font-size: 14px;position: absolute;left: 0;right: 0;top: 50%;margin: 30px auto;">' + (title || "鎷煎懡鍔犺浇涓仿仿�") + '</div>';
    dom.appendChild(div);
}
function closeLoading() {
    var loadingDom = document.getElementsByClassName('x-loading')[0];
    if (loadingDom) {
        loadingDom && loadingDom.parentNode.removeChild(loadingDom);
        loadingDom.style.overflow = 'inherit';
        document.body.style.overflow = 'inherit';
    }
}
function getUserIpAddr(callback) {
    $.getScript("https://pv.sohu.com/cityjson?ie=utf-8", function (res, status) {
        if (status === 'success') {
            callback && callback(returnCitySN.cip);
        } else {
            callback && callback('125.119.99.166');
        }
    });
}
function setLocation(data) {
    localStorage.locationInfo = JSON.stringify(data);
}
function getLocation(callBack) {
    var locatioInfo = localStorage.locationInfo;
    if (locatioInfo) {
        locatioInfo = JSON.parse(locatioInfo);
        callBack && callBack(locatioInfo);
        return locatioInfo;
    }
    getUserIpAddr(function (ip) {
        $.ajax({
            url: 'https://api.map.baidu.com/location/ip?ip=' + ip,
            type: 'get',
            dataType: 'jsonp',
            data: {
                ak: 'hEG2iYWixHa2kdru3z48LzaYTnjNKawK'
            },
            success: function success(data) {
                /* requestAjax({
                    url: '/lfh_front/getCityId',
                    async: false,
                    data: {
                        areaName: data.content.address_detail.city
                    },
                    success: function (res) {
                        const cityInfo = {
                            city: data.content.address_detail.city,
                            point: data.content.point,
                            id: res.id
                        }
                        localStorage.locationInfo = JSON.stringify(cityInfo)
                        locatioInfo = cityInfo
                        callBack && callBack(locatioInfo)
                    }
                }) */
            },
            error: function error() {
                // 澶辫触榛樿瀹氫綅鏉窞
                var locatioInfo = {};
                callBack && callBack(locatioInfo);
            }
        });
    });
    return locatioInfo;
}
// 鏃堕棿鏍煎紡鍖�
function getFormaTime(fmt, time) {
    var time = time || new Date();
    var o = {
        "M+": time.getMonth() + 1, //鏈堜唤
        "d+": time.getDate(), //鏃�
        "H+": time.getHours(), //灏忔椂
        "m+": time.getMinutes(), //鍒�
        "s+": time.getSeconds(), //绉�
        "q+": Math.floor((time.getMonth() + 3) / 3), //瀛ｅ害
        "S": time.getMilliseconds() //姣
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }return fmt;
}

//鍌ㄥ瓨localStorage
// 鏈夋晥鏈熼粯璁や负涓冨ぉ銆備紶鍏ョ殑杩囨湡鏃堕棿涓虹
function setLocalStorage(name, value, expressTime) {
    var time = expressTime || 7 * 24 * 60 * 60;
    var express = Math.floor(+new Date() / 1000) + time;
    var data = {
        value: value,
        express: express
    };
    localStorage.setItem(name, JSON.stringify(data));
}
// 鑾峰彇localStorage
// 杩囨湡鎴栬€呬笉浼爊ame杩斿洖 鈥欌€�
function getLocalStorage(name) {
    if (!name) return '';
    var currTime = Math.floor(+new Date() / 1000);
    var data = JSON.parse(localStorage.getItem(name));
    if (!data) return '';
    var expressTime = data.express;
    if (currTime > expressTime) {
        localStorage.removeItem(name);
        return '';
    } else {
        return data.value;
    }
}

// 鐢熸垚闅忔満瀛楃涓� 榛樿鍗佷綅
function getRadomStr(len) {
    var slen = len || 10;
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < len; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
}

//鑾峰彇娴忚鍣ㄧ被鍨�
function getExplorer() {
    var explorer = window.navigator.userAgent;
    if (explorer.indexOf("MSIE") >= 0) {
        return "IE";
    } else if (explorer.indexOf("Firefox") >= 0) {
        return "Firefox";
    } else if (explorer.indexOf("MicroMessenger") >= 0) {
        return "Wechat";
    } else if (explorer.indexOf("Chrome") >= 0) {
        return "Chrome";
    } else if (explorer.indexOf("Opera") >= 0) {
        return "Opera";
    } else if (explorer.indexOf("Safari") >= 0) {
        return "Safari";
    } else {
        return "Unknown";
    }
}

// 鎴彇鍦板潃鏍忎腑鐨勫弬鏁� 榛樿涓嶄紶鍙傝繑鍥炴墍鏈夊弬鏁扮粍鎴愮殑瀵硅薄锛�
// 鍙傛暟 options 鍙┛ 鎸囧畾url杩涜鎴彇  鍙互绌� name 杩斿洖鎸囧畾鐨勫弬鏁板悕
function getUrlParam(options) {
    var option = options || {};
    var url = option.url || window.location.href;
    // const urlParam = url.split('#')[1]
    var paramStr = url.split('?')[1];
    if (paramStr) {
        var paramArr = paramStr.split('&');
        var paramObj = {};
        paramArr.forEach(function (item) {
            var itemArr = item.split('=');
            var key = itemArr[0];
            var value = decodeURIComponent(itemArr[1]);
            paramObj[key] = value;
        });
        if (option.name) {
            return paramObj[option.name];
        } else {
            return paramObj;
        }
    } else {
        return {};
    }
}

// 缁熻瀛楁鍦ㄦ暟缁勪腑鍑虹幇鐨勬鏁�
function countTimes(arr, num) {
    var total = 0;
    for (var i = arr.length - 1; i >= 0; i--) {
        arr[i] === num ? total++ : '';
    }
    return total;
}

// 缁熻瀛楁鍦ㄦ暟缁勪腑鍑虹幇鐨勬鏁�
function getArrMaxOrMin(arr, type) {
    var sortArr = arr.sort(function (a, b) {
        // b-a浠庡ぇ鍒板皬锛宎-b浠庡皬鍒板ぇ
        return b - a;
    });
    if (type === 'max') {
        // 澶�
        return sortArr[0];
    } else {
        // 灏�
        return sortArr[sortArr.length - 1];
    }
}

// base 64鍥剧墖杞� Blob瀵硅薄
function convertBase64UrlToBlob(urlData) {
    var bytes = window.atob(urlData.split(',')[1]); //鍘绘帀url鐨勫ご锛屽苟杞崲涓篵yte
    //澶勭悊寮傚父,灏哸scii鐮佸皬浜�0鐨勮浆鎹负澶т簬0
    var ab = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
    }
    return new Blob([ab], { type: 'image/png' });
}

// 楠岃瘉瑙勫垯
var verifyRules = {
    // 鎵嬫満鍙烽獙璇�
    verifyPhone: function verifyPhone(value) {
        if (value) {
            if (/^1[3|4|5|6|7|8|9]\d{9}$/.test(value)) {
                return true;
            } else {
                layer.msg('鎵嬫満鍙疯緭鍏ヤ笉鍚堟硶');
                return false;
            }
        } else {
            layer.msg('璇疯緭鍏ユ墜鏈哄彿');
        }
    },
    // 鍥惧舰楠岃瘉鐮佹牎楠�
    verifyImgCaptcha: function verifyImgCaptcha(value) {
        if (value) {
            if (/^[0-9a-zA-Z]{4}$/.test(value)) {
                return true;
            } else {
                layer.msg('鍥惧舰楠岃瘉鐮佽緭鍏ヤ笉鍚堟硶');
                return false;
            }
        } else {
            layer.msg('璇疯緭鍏ュ浘褰㈤獙璇佺爜');
        }
    },
    verifyPassword: function verifyPassword(value) {
        if (value) {
            if (/^(\w){4,20}$/.test(value)) {
                return true;
            } else {
                layer.msg('璇疯緭鍏�4-20闀垮害鐨勫瘑鐮侊紝鍙兘鐢卞瓧姣嶃€佹暟瀛椼€佷笅鍒掔嚎缁勬垚');
                return false;
            }
        } else {
            layer.msg('璇疯緭鍏ュ瘑鐮�');
        }
    },
    verifyCaptcha: function verifyCaptcha(value) {
        if (value) {
            if (/^[0-9]*$/.test(value)) {
                return true;
            } else {
                layer.msg('鎵嬫満楠岃瘉鐮佽緭鍏ヤ笉鍚堟硶');
                return false;
            }
        } else {
            layer.msg('璇疯緭鍏ユ墜鏈洪獙璇佺爜');
        }
    }
};